package com.nep.NEPM.utils;


import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class FileUtils {
    public static Map readMapObject(String fileName){
        File file = new File(fileName);
        Map map = null;
        try{
            //文件为空时创建map
            if(file.length()==0){
                map = new HashMap<>();
            }else{
                FileInputStream fis = new FileInputStream(file);
                ObjectInputStream ois = new ObjectInputStream(fis);
                map = (Map)ois.readObject();

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return map;
    }

    public static void writeMapObject(String fileName,Map map) throws IOException {
        File file = new File(fileName);
        if(!file.exists()){
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(map);
        oos.flush();



    }


}
